package com.jiguang.shangcheng.servlet;

import com.jiguang.shangcheng.dao.ShangChengDao;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/shang/updatePwd")
public class updatePwd extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");

        //获取数据
        String number = req.getParameter("number");
        String pwd = req.getParameter("pwd");
        String password = req.getParameter("password");

        //判断用户名和密码是否正确
        ShangChengDao shangChengDao = new ShangChengDao();
        boolean login = shangChengDao.login(number, pwd);
        if(!login){
            //登录失败
            PrintWriter out = resp.getWriter();
            out.println("<script>alert('账号或密码错误');location.href='/shang/updatePwd.jsp';</script>");
            out.close();
            return;
        }else{
            //修改密码
            boolean result = shangChengDao.updatePwd(number,password);
            if(result){
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('修改成功');location.href='/shang/login.jsp';</script>");
                out.close();
            }else{
                //登录失败
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('修改错误');location.href='/shang/updatePwd.jsp';</script>");
                out.close();
            }
        }
    }
}
